package com.jdl.iscm.driver;

import lombok.Data;
import org.openqa.selenium.WebDriver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Ran
 * @Date: 2021/8/16
 **/
@Data
@Configuration
@PropertySource(value = "classpath:driver.properties")
@ConfigurationProperties(prefix = "chrome")
public class ChromeDriver {

    private String driver;

    private String start;

    private List<String> args;

    private Boolean headless;

    /**
     * 初始化Chrome配置
     *
     * @return WebDriver
     */
    public WebDriver chromeDriver() {
        System.setProperty("webdriver.chrome.driver", this.driver);
        ChromeOptions options = new ChromeOptions();
        options.setBinary(this.start);
        this.args.forEach(options::addArguments);
        // 禁用保存密码提示框
        options.setExperimentalOption("prefs", new HashMap<String, Object>() {{
            put("credentials_enable_service", false);
            put("profile.password_manager_enabled", false);
        }});
        WebDriver driver = new org.openqa.selenium.chrome.ChromeDriver(options);
        //隐式等待
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        return driver;
    }
}
